<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Usermodel extends CI_Model {

	protected $_CI;

	public static $searchField = array(
		'group_id' => 'group_id',
		'user_status' => 'user_status',
	);
	public static $likeField = array(
		'user_code' => 'user_code',
		'user_name' => 'user_name',
		'user_phone' => 'user_phone',
	);
	public static $sortField = array(
		'user_name' => 'user_name',
	);

	public function __construct(){
		parent::__construct();
		$this->_CI = & get_instance();
	}
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
	public function getUserListSection($assist){
		if(!empty($assist['select'])) $this->db->select($assist['select']);
		$this->db->from('user');
		$this->db->where('user_status !=',STATUS_DELETE);
		$this->db->where('user_id !=',1);
		if(!empty($assist['where'])) $this->db->where($assist['where']);
		if(!empty($assist['like'])) $this->db->like($assist['like']);
		$count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

		if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
		$this->db->order_by('user_status','desc');
		if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
		$query = $this->db->get();
		$list = $query->result_array();

		return array($list,$count);
	}

	public function getUserByCode($user_code){
		$this->db->from('user');
		$this->db->where('user_code',$user_code);
		$this->db->limit(1);
		$query = $this->db->get();
		$res = $query->row_array();

		return $res;
	}

	public function getUserById($user_id){
		if(!is_array($user_id)) $user_id = array($user_id);
		if(empty($user_id)) return array();
		
		$this->db->from('user');
		$this->db->where_in('user_id',$user_id);
		$query = $this->db->get();
		$list = $query->result_array();
		
		return $list;
	}
/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
	public function addUser($info) {
		$db_debug = $this->db->db_debug;
		$this->db->db_debug = FALSE; 
		$m = $this->db->insert('user',$info);
		$id = $this->db->insert_id();
		$this->db->db_debug = $db_debug;
		return $m ? $id : 0;
	}
/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
	public function editUser($info){
		$db_debug = $this->db->db_debug;
		$this->db->db_debug = FALSE; 
		$this->db->where('user_id',$info['user_id']);
		unset($info['user_id']);
		$m = $this->db->update('user',$info);
		$this->db->db_debug = $db_debug;
		return $m == true;
	}
/*
| -------------------------------------------------------------------
|  CHECK Functions
| -------------------------------------------------------------------
*/
	public function checkNameAvailable($name,$user_id = 0){
		$this->db->from('user');
		$this->db->where('user_id !=',$user_id);
		$this->db->where('user_name',$name);
		$count = $this->db->count_all_results();

		return ($count == 0);
	}

	public function checkCodeAvailable($code,$user_id = 0){
		$this->db->from('user');
		$this->db->where('user_id !=',$user_id);
		$this->db->where('user_code',$code);
		$count = $this->db->count_all_results();

		return ($count == 0);
	}

}